{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Python原生的list\n",
    "# 假设有两个list\n",
    "a = [1, 2, 3, 4, 5]\n",
    "b = [2, 3, 4, 5, 6]\n",
    "\n",
    "# 完成如下计算\n",
    "# 对a的每个元素 + 1\n",
    "# a = a + 1 不能这么写，会报错\n",
    "# a[:] = a[:] + 1 也不能这么写，也会报错\n",
    "for i in range(5):\n",
    "    a[i] = a[i] + 1\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用ndarray\n",
    "import numpy as np\n",
    "a = np.array([1, 2, 3, 4, 5])\n",
    "a = a + 1\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算 a和b中对应位置元素的和，是否可以这么写？\n",
    "a = [1, 2, 3, 4, 5]\n",
    "b = [2, 3, 4, 5, 6]\n",
    "c = a + b\n",
    "# 检查输出发现，不是想要的结果\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3,  5,  7,  9, 11])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用numpy中的ndarray完成两个ndarray相加\n",
    "import numpy as np\n",
    "a = np.array([1, 2, 3, 4, 5])\n",
    "b = np.array([2, 3, 4, 5, 6])\n",
    "c = a + b \n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5,  8, 11, 14, 17],\n",
       "       [10, 13, 16, 19, 22]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 自动广播机制，1维数组和2维数组相加\n",
    "\n",
    "# 二维数组维度 2x5\n",
    "# array([[ 1,  2,  3,  4,  5],\n",
    "#         [ 6,  7,  8,  9, 10]])\n",
    "d = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])\n",
    "# c是一维数组，维度5\n",
    "# array([ 4,  6,  8, 10, 12])\n",
    "c = np.array([ 4,  6,  8, 10, 12])\n",
    "e = d + c\n",
    "e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,2,3,4,5,6]\n",
    "b = np.array(a)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 4, 6, 8])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过np.arange创建\n",
    "# 通过指定start, stop (不包括stop)，interval来产生一个1维的ndarray\n",
    "a = np.arange(0,10,2)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建全0的ndarray\n",
    "a = np.zeros([3,3])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建全1的ndarray\n",
    "a = np.ones([3,3])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a, dtype: float64, shape: (3, 3), size: 9, ndim: 2\n"
     ]
    }
   ],
   "source": [
    "# shape：数组的形状 ndarray.shape，1维数组（N, ），二维数组（M, N），三维数组（M, N, K）。\n",
    "# dtype：数组的数据类型。\n",
    "# size：数组中包含的元素个数 ndarray.size，其大小等于各个维度的长度的乘积。\n",
    "# ndim：数组的维度大小，ndarray.ndim, 其大小等于ndarray.shape所包含元素的个数。\n",
    "a = np.ones([3, 3])\n",
    "print('a, dtype: {}, shape: {}, size: {}, ndim: {}'.format(a.dtype, a.shape, a.size, a.ndim))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 0.5       , 0.33333333],\n",
       "       [0.25      , 0.2       , 0.16666667]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标量除以数组，用标量除以数组的每一个元素\n",
    "arr = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "1. / arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.,  4.,  6.],\n",
       "       [ 8., 10., 12.]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标量乘以数组，用标量乘以数组的每一个元素\n",
    "arr = np.array([[1., 2., 3.], [4., 5., 6.]])\n",
    "2.0 * arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3., 4., 5.],\n",
       "       [6., 7., 8.]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标量加上数组，用标量加上数组的每一个元素\n",
    "arr = np.array([[1., 2., 3.], [4., 5., 6.]])\n",
    "2.0 + arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  0., -1.],\n",
       "       [-2., -3., -4.]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标量减去数组，用标量减去数组的每一个元素\n",
    "arr = np.array([[1., 2., 3.], [4., 5., 6.]])\n",
    "2.0 - arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-10., -10., -10.],\n",
       "       [-17., -17., -17.]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组 减去 数组， 用对应位置的元素相减\n",
    "arr1 = np.array([[1., 2., 3.], [4., 5., 6.]])\n",
    "arr2 = np.array([[11., 12., 13.], [21., 22., 23.]])\n",
    "arr1 - arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[12., 14., 16.],\n",
       "       [25., 27., 29.]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组 加上 数组， 用对应位置的元素相加\n",
    "arr1 = np.array([[1., 2., 3.], [4., 5., 6.]])\n",
    "arr2 = np.array([[11., 12., 13.], [21., 22., 23.]])\n",
    "arr1 + arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 11.,  24.,  39.],\n",
       "       [ 84., 110., 138.]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组 乘以 数组，用对应位置的元素相乘\n",
    "arr1 * arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.09090909, 0.16666667, 0.23076923],\n",
       "       [0.19047619, 0.22727273, 0.26086957]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组 除以 数组，用对应位置的元素相除\n",
    "arr1 / arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 1.41421356, 1.73205081],\n",
       "       [2.        , 2.23606798, 2.44948974]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组开根号，将每个位置的元素都开根号\n",
    "arr ** 0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(30)\n",
    "a\n",
    "a[10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3, 10, 10, 10,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#将一个标量值赋值给一个切片时，该值会自动传播到整个选区。\n",
    "a = np.arange(30)\n",
    "a[4:7] = 10\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([  0,   1,   2,   3, 100,   5,   6,   7,   8,   9,  10,  11,  12,\n",
       "         13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,\n",
       "         26,  27,  28,  29]),\n",
       " array([100,   5,   6]))"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组切片产生的新数组，还是指向原来的内存区域，数据不会被复制。\n",
    "# 视图上的任何修改都会直接反映到源数组上。\n",
    "a = np.arange(30)\n",
    "arr_slice = a[4:7]\n",
    "arr_slice[0] = 100\n",
    "a, arr_slice"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
